﻿using System;
using System.Xml.Linq;

namespace Game
{
    public class MoneyEditDialog : Dialog
    {
        public ButtonWidget m_okButton;

        public ListPanelWidget m_itemsList;

        public Action<int> m_handler;

        public int? value;

        public MoneyEditDialog(Action<int> handler)
        {
            XElement node = ContentManager.Get<XElement>("Dialogs/MoneyEditDialog");
            LoadContents(this, node);
            m_okButton = Children.Find<ButtonWidget>("OkButton");
            m_itemsList = Children.Find<ListPanelWidget>("ItemsList");
            m_handler = handler;
            m_itemsList.ItemWidgetFactory = delegate (object item)
            {
                Money money2 = (Money)item;
                int num = Terrain.ExtractContents(money2.ResultValue);
                Block block = BlocksManager.Blocks[num];
                XElement node2 = ContentManager.Get<XElement>("Widgets/MoneyItem");
                ContainerWidget containerWidget = (ContainerWidget)Widget.LoadWidget(this, node2, null);
                containerWidget.Children.Find<BlockIconWidget>("Icon").Value = money2.ResultValue;
                containerWidget.Children.Find<LabelWidget>("Name").Text = "商品名称:" + block.GetDisplayName(null, money2.ResultValue);
                containerWidget.Children.Find<LabelWidget>("Money").Text = "商品价格:" + money2.itemMoney + "   售价:" + money2.returnMoney;
                return containerWidget;
            };
            ListPanelWidget itemsList = m_itemsList;
            itemsList.ItemClicked = (Action<object>)Delegate.Combine(itemsList.ItemClicked, (Action<object>)delegate (object item)
            {
                Money money = (Money)item;
                value = money.ResultValue;
            });
            foreach (Money recipe in MoneyManager.m_recipes)
            {
                m_itemsList.AddItem(recipe);
            }
        }

        public override void Update()
        {
            if (m_okButton.IsClicked)
            {
                DialogsManager.HideDialog(this);
                if (m_handler != null && value.HasValue)
                {
                    m_handler(value.Value);
                }
            }
        }
    }
}